package apis.youtube
{
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import bi.conf.Configuration;
	import flash.net.URLLoader;
	import flash.events.EventDispatcher;
	
	
	/**
	 * recuere la liste des video d'un user de YouTube
	 * @author Pierr Lepers
	 * 
	 */	
	public class YouTubeUserList extends YouTubeConnector
	{
		
		public static const FAVORITES : String = "fav";
		public static const OWNED : String = "owned";
		
		
		function YouTubeUserList( type : String ) : void {
			if( type == FAVORITES ) method = "youtube.users.list_favorite_videos";
			else 					method = "youtube.videos.list_by_user";
		}
		
		public function grabInfos( user : String ) : void {
			var req : URLRequest = new URLRequest( Configuration.getInstance().getString("youtubeWsUrl") );
			var variables : URLVariables = new URLVariables();
			variables.method = method;
			variables.dev_id = Configuration.getInstance().getString( "youtubeDevId" );
			variables.user = user;
			
			req.data = variables;
			
			trace( variables.dev_id );
			
			var dataLoader : URLLoader = new URLLoader();
			dataLoader.addEventListener( Event.COMPLETE, onComplete ) ;
			dataLoader.addEventListener( ErrorEvent.ERROR, onError ) ;
			dataLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHttpStatus );
			dataLoader.load( req );
		}
		
		//_______________________________________________________________________
		//																PRIVATES
		
		
		override protected function onComplete( e : Event ) : void {
			 var l : URLLoader = e.target as URLLoader;
			 _datas = XML( l.data );
			 
			 parseDatas();
		}
		override protected function onError( e : ErrorEvent ) : void {
			 trace( e.text );
		}
		override protected function onHttpStatus( e : HTTPStatusEvent ) : void {
			 trace("********************STATUS DE YOUTUBE*************************");
			 trace( e.status );
		}
		
		override protected function parseDatas() : void {
			 
			trace( _datas );

			dispatchEvent( new Event( Event.COMPLETE ) );
		}
		
		private var method : String;
	}
}